<?php

namespace App\Miya\UIData;

use App\Miya\Models\Area;
use App\Miya\Models\DerivativeKeyword;
use App\Miya\Models\Keyword;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Collection;
use Exception;

class KeywordData extends CommonData
{
    protected $name = 'keyword';
    protected $fields = ['id', 'keyword'];

    /**
     * 得到关键词信息
     * @param $id 关键词id
     * @param bool $need_derivatives 是否需要衍生词 传入true才可以遍历derivatives
     *
     * @operate 直接调用
     *
     * @data id 关键词id
     * @data keyword 关键词
     * @data derivatives? 衍生关键词 遍历后调用
     */
    public function get($id, $need_derivatives = false): Keyword
    {
        if ($need_derivatives) {
            $keyword = Keyword::select($this->fields)->with('derivatives:id,keyword_id,keyword')->find($id);
        } else {
            $keyword = Keyword::select($this->fields)->find($id);
        }
        if (!$keyword) {
            $msg = sprintf('关键词信息中未找到id为 %s 的数据信息', $id);
            throw new ModelNotFoundException($msg);
        }

        return $keyword;
    }

    /**
     * 返回主关键词字符串
     * @operate 直接调用
     */
    public function list(): Collection
    {
        return $this->dataCache(function () {
            return Keyword::pluck('keyword');
        }, __METHOD__);
    }

    /**
     * 返回指定地区/主关键词 衍生词字符串
     * @param Area $area 地区对象
     * @param Keyword $keyword 主关键词对象
     * @param arrya $type 衍生关键词类型
     *
     * @operate 直接调用
     */
    public function derivatives(Area $area = null, Keyword $keyword = null, array $type = []): Collection
    {
        $derivatives = DerivativeKeyword::where(function ($query) use ($area, $keyword, $type) {
            if ($area) {
                $query->where('area_id', $area->id);
            }

            if ($keyword) {
                $query->where('keyword_id', $keyword->id);
            }

            if ($type != []) {
                $query->whereIn('type', $type);
            } else {
                $query->whereIn('type', explode(',', config('keyword.derivative_types')));
            }
        })->pluck('keyword');

        if ($derivatives->count() <= 0) {
            throw new Exception('未设置衍生关键词,请到后台设置!');
        }

        return $derivatives;
    }

    /**
     * 返回指定地区/主关键词 衍生词字符串
     * @param Area $area 地区对象
     * @param Keyword $keyword 主关键词对象
     * @param arrya $type 衍生关键词类型
     * @param num 随机查询关键词数量
     *
     * @operate 直接调用
     */

    public function derivativesByRank(Area $area = null, Keyword $keyword = null, array $type = [], $num = 10): Collection
    {
        return DerivativeKeyword::where(function ($query) use ($area, $keyword, $type) {
            if ($area) {
                $query->where('area_id', $area->id);
            }

            if ($keyword) {
                $query->where('keyword_id', $keyword->id);
            }

            if ($type != []) {
                $query->whereIn('type', $type);
            } else {
                $query->whereIn('type', explode(',', config('keyword.derivative_types')));
            }
        })->inRandomOrder()->take($num)->pluck('keyword');
    }
}
